#!/usr/bin/env python
# -*- coding: utf-8 -*-
# datetime: 15/4/2023 下午11:14
# Author: name
# File: data_encode_decode.py
# ide: PyCharm
# description:
from base64 import b64encode, b64decode
from hashlib import sha256, sha1, sha224, sha384, sha512, md5
from urllib.parse import quote, unquote
from gmssl import sm3
from pywebio.output import put_html, put_buttons, put_button, put_tabs
from pywebio.pin import put_textarea, pin_update, pin

from commons.comms import check_actually_data
from frontend.menus import show_menus


def clear_textarea_data():
    pin_update("actually_data", value="")
    pin_update("encryption_data", value="")


def data_encode_decode(type):
    data = pin.actually_data
    check_result = check_actually_data(data)
    if not check_result:
        if type == "sha1":
            pin_update("encryption_data", value=sha1(data.encode("utf-8")).hexdigest())
        elif type == "sha256":
            pin_update("encryption_data", value=sha256(data.encode("utf-8")).hexdigest())
        elif type == "sha224":
            pin_update("encryption_data", value=sha224(data.encode("utf-8")).hexdigest())
        elif type == "sha384":
            pin_update("encryption_data", value=sha384(data.encode("utf-8")).hexdigest())
        elif type == "sha512":
            pin_update("encryption_data", value=sha512(data.encode("utf-8")).hexdigest())
        elif type == "md5":
            pin_update("encryption_data", value=md5(data.encode("utf-8")).hexdigest())
        elif type == "encode":
            pin_update("encryption_data", value=quote(data.encode("utf-8")))
        elif type == "decode":
            pin_update("encryption_data", value=unquote(data.encode("utf-8")))
        elif type == "base64encode":
            pin_update("encryption_data", value=str(b64encode(data.encode("utf-8")))[2:-1])
        elif type == "base64decode":
            try:
                pin_update("encryption_data", value=b64decode(data).decode("utf-8"))
            except:
                pin_update("encryption_data", value=data)
        elif type == "sm3":
            pin_update("encryption_data", value=sm3.sm3_hash([i for i in data.encode()]).upper())
        else:
            pass


def html_data_encode_decode():
    """
    数据加密解密

    :return:
    """
    show_menus()
    put_tabs([
        {
            "title": "加密解密",
            "content": [
                put_html("<h4>加密方式</h4>"),
                put_buttons(
                    ["sha1", "sha256", "sha224", "sha384", "sha512", "md5", "urlEncode", "urlDecode",
                     "base64加密", "base64解密", "sm3"],
                    onclick=[
                        lambda: data_encode_decode("sha1"),
                        lambda: data_encode_decode("sha256"),
                        lambda: data_encode_decode("sha224"),
                        lambda: data_encode_decode("sha384"),
                        lambda: data_encode_decode("sha512"),
                        lambda: data_encode_decode("md5"),
                        lambda: data_encode_decode("encode"),
                        lambda: data_encode_decode("decode"),
                        lambda: data_encode_decode("base64encode"),
                        lambda: data_encode_decode("base64decode"),
                        lambda: data_encode_decode("sm3")
                    ], small=True),
                put_html("<h4>铭文数据:</h4>"),
                put_button("清空数据", onclick=clear_textarea_data, small=True),
                put_textarea("actually_data"),
                put_html("<h4>加密数据:</h4>"),
                put_textarea("encryption_data")

            ],
        }
    ], scope="data_list")
